package org.hank.wanandroid.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.fragment_project.*
import org.hank.commom.base.BaseFragment
import org.hank.wanandroid.R
import org.hank.wanandroid.adapter.MyFragmentAdapter
import org.hank.wanandroid.bean.ProjectBean
import org.hank.wanandroid.viewmodel.ProjectViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/5/26 17:55
 *描述:
 */
class ProjectPageFragment : BaseFragment(R.layout.fragment_project) {
    var pageListData: MutableList<ProjectBean> = ArrayList()
    lateinit var pageAdapter: MyFragmentAdapter<ProjectPageFragment,ProjectBean>
    var lastSelectedPosition = -1

    val viewModel by getViewModel(ProjectViewModel::class.java) {
        projectBeanLiveData.observe(it) {
            pageListData.addAll(it)
            pageAdapter.notifyDataSetChanged()
        }
    }

    lateinit var tablayoutMediator: TabLayoutMediator

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //查询所有项目
        viewModel.getProjectList()
        pageAdapter = MyFragmentAdapter(this@ProjectPageFragment, pageListData)
        viewpager2_tab_article_project.apply {
            adapter = pageAdapter
            offscreenPageLimit = 2
            registerOnPageChangeCallback(viewPager2CallBack)
            isSaveEnabled = false
        }
        viewpager2_tab_article_project.post {
            tablayoutMediator = TabLayoutMediator(
                tablayout_tab_article_project,
                viewpager2_tab_article_project
            ) { tab, position ->
                tab.text = pageListData[position].name
            }
            tablayoutMediator.attach()
        }
        tablayout_tab_article_project.addOnTabSelectedListener(object :
            TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.apply {
                    if (Math.abs(position - lastSelectedPosition) > 4)
                        viewpager2_tab_article_project.setCurrentItem(position, false)
                    else
                        viewpager2_tab_article_project.setCurrentItem(position, true)
                    lastSelectedPosition = position
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
    }

    var viewPager2CallBack = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            tablayout_tab_article_project.setScrollPosition(
                position,
                positionOffset,
                true,
                true
            )
        }

        override fun onPageSelected(position: Int) {
            tablayout_tab_article_project.selectTab(
                tablayout_tab_article_project.getTabAt(
                    position
                )
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        viewpager2_tab_article_project.unregisterOnPageChangeCallback(viewPager2CallBack)
        tablayoutMediator.detach()
    }

}